package com.mdlib.helper.module.send.profile.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.ljlib.core.util.EmptyUtils;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.base.BaseTitleFragment;
import com.mdlib.helper.model.BaseResult;
import com.mdlib.helper.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 作者：HQY on 16/11/16 10:11
 * 邮箱：hqy_xz@126.com
 */

public class ModifyNameFragment extends BaseTitleFragment {
    private static String NAME = "name";
    @Bind(R.id.et_name)
    EditText mEtName;
    @Bind(R.id.tv_name_ok)
    TextView mTvNameOk;

    private String nickName;

    public static ModifyNameFragment newInstance(String name) {
        ModifyNameFragment fragment = new ModifyNameFragment();
        Bundle bundle = new Bundle();
        bundle.putString(NAME, name);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (EmptyUtils.isNotEmpty(getArguments())) {
            nickName = getArguments().getString(NAME);
        }
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        setTitle(R.string.nickname_tv_modify_title, R.color.white)
                .setTitleBgColor(R.color.generic_view_bg)
                .setImgLeftListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        removeFragment();
                    }
                });
        mEtName.setText(nickName);
    }


    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_modify_nickname;
    }


    @OnClick(R.id.tv_name_ok)
    public void onClick() {
        String name = mEtName.getText().toString();
        if (EmptyUtils.isNotEmpty(name)) {
            startLoadingDialog(true);
            addSubscribe(DataManager.getInstance().getUserApi()
                    .modifyNickName(mEtName.getText().toString())
                    .compose(RxHelper.<BaseResult>rxSchedulerHelper())
                    .subscribe(new RxSubscribe<BaseResult>() {
                        @Override
                        protected void onSucc(BaseResult baseResult) {
                            stopLoadingDialog();
                            ToastUtil.showToast(baseResult.msg);
                            removeFragment();
                        }

                        @Override
                        protected void onFail(String msg) {
                            stopLoadingDialog();
                            ToastUtil.showToast(msg);
                        }
                    }));
        } else {
            ToastUtil.showToast(getActivity().getResources().getString(R.string.generic_et_nickname_not_empty));
        }
    }
}
